/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ToxicBakery.pagetransformer.page;

import com.ToxicBakery.pagetransformer.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 页面适配器
 *
 * @since 2021-04-12
 */
public class PageProvider extends PageSliderProvider {
    private static final int TEXT_SIZE = 70;
    private static final int COLOR1 = -0xcc4a1b;
    private static final int COLOR2 = -0x559934;
    private static final int COLOR3 = -0x663400;
    private static final int COLOR4 = -0x44cd;
    private static final int COLOR5 = -0xbbbc;

    private LayoutScatter layoutScatter;
    private List<Integer> colors = new ArrayList<>();
    private ArrayList<Component> components = new ArrayList<>();

    /**
     * 创建PageSlide适配器
     *
     * @param context
     */
    public PageProvider(Context context) {
        layoutScatter = LayoutScatter.getInstance(context);
        colors.add(COLOR1);
        colors.add(COLOR2);
        colors.add(COLOR3);
        colors.add(COLOR4);
        colors.add(COLOR5);
    }

    @Override
    public int getCount() {
        return colors.size();
    }

    /**
     * 获取Child 没有
     *
     * @param componentContainer componentContainer
     * @param position position
     * @return Object
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        Component page1 = layoutScatter.parse(ResourceTable.Layout_page_slide_layout, null, false);
        Text tv = (Text) page1.findComponentById(ResourceTable.Id_tv);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(colors.get(position)));
        page1.setBackground(element);
        tv.setText((position + 1) + "");
        tv.setTextColor(Color.WHITE);
        tv.setTextSize(AttrHelper.fp2px(TEXT_SIZE, componentContainer.getContext()));
        tv.setTextAlignment(TextAlignment.CENTER);
        componentContainer.addComponent(page1);
        components.add(position, page1);
        return page1;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    public List<Component> getComponents() {
        return components;
    }
}
